package com.dhnsoft.game;

import com.dhnsoft.tank.Tank;
import com.dhnsoft.util.Constant;

import java.awt.*;


/**
 * @Description
 * @ClassName Bullet
 * @Author dhn
 * @date 2020.11.18 16:17
 */
//子弹类
public class Bullet {
    private static Image[] bulletImag;
    static {
        bulletImag=new Image[4];
        bulletImag[0]=Toolkit.getDefaultToolkit().createImage("images/bulletU.png");
        bulletImag[1]=Toolkit.getDefaultToolkit().createImage("images/bulletD.png");
        bulletImag[2]=Toolkit.getDefaultToolkit().createImage("images/bulletL.png");
        bulletImag[3]=Toolkit.getDefaultToolkit().createImage("images/bulletR.png");
    }
    //子弹的默认的速度为坦克速度的两倍
    public static final int DEFAULT_SPEED= 20;
    //炮弹的半径
    public static final int RADIUS=4;
    private int x,y;
    private int speed=DEFAULT_SPEED;
    private int dir;
    //伤害
    private int atk;
    private Color color;
    //子弹是否可见
    private boolean visible=true;
    public Bullet(int x, int y, int dir, int atk) {
        this.x = x;
        this.y = y;
        this.dir = dir;
        this.atk = atk;
        color=Color.blue;
    }

    public Bullet() {
    }

    /**
     * 炮弹的自身的绘制的方法
     * */
    public void draw(Graphics g){
        //当子弹不可见时不画子弹
        if(!visible){
            return;
        }
        logic();
        //g.setColor(color);
        g.drawImage(bulletImag[dir],x-RADIUS,y-RADIUS,null);
//        for(int i=0;i<4;i++){
//            g.drawImage(bulletImag[i],x-RADIUS,y-RADIUS,null);
//        }
    }
    /**
     * 子弹的逻辑
     * */
    private void logic(){
        move();
    }
    private void move(){
        switch (dir){
            case Tank.DIR_UP:
                y-=speed;
                if(y<=0){
                    visible=false;
                }
                break;
            case Tank.DIR_DOWN:
                y+=speed;
                if(y> Constant.FRAME_HEIGHT){
                    visible=false;
                }
                break;
            case Tank.DIR_LEFT:
                x-=speed;
                if(x<=0){
                    visible=false;
                }
                break;
            case Tank.DIR_RIGHT:
                x+=speed;
                if(x>Constant.FRAME_WIDTH){
                    visible=false;
                }
                break;

        }
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getDir() {
        return dir;
    }


    public void setDir(int dir) {
        this.dir = dir;
    }

    public int getSpeed() {
        return speed;
    }

    public void setSpeed(int speed) {
        this.speed = speed;
    }

    public int getAtk() {
        return atk;
    }

    public void setAtk(int atk) {
        this.atk = atk;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }
    /**
     * 坦克的功能，坦克开火的方法
     * */

}
